<template>
  <div>
    <header-title :header-title="title" />
    <customer-form @on-success-valid="handleSubmit" />
    <customer-dialog
      :dialog-visible="visible"
      :row="row"
      @cancel-dialog="handleCancel"
      @confirm-dialog="handleConfirm"
    />
    <el-table
      v-loading="loading"
      :data="CustomerTableData()"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        type="index"
        :index="getIndex"
        label="序列"
        width="80"
        align="center"
      />
      <el-table-column
        prop="name"
        label="客户姓名"
        min-width="130"
        align="center"
      />
      <el-table-column
        prop="tel"
        label="客户电话"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="type0"
        label="喜欢车型"
        min-width="150"
        align="center"
      />
      <el-table-column
        prop="ctime"
        label="创建时间"
        min-width="180"
        align="center"
      />
      <el-table-column
        label="预约时间"
        min-width="180"
        align="center"
      >
        <template slot-scope="scope">
          <div>
            {{
              scope.row.ordertime ? $moment(scope.row.ordertime).format('YYYY-MM-DD HH:mm:ss') : '-'
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="des"
        label="备注"
        min-width="180"
        align="center"
      />
      <el-table-column
        label="操作"
        min-width="150"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="handleClick(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="CustomerDataCount()"
      @click-page="handleClickPage"
      @change-pagesize="handleSizeChange"
    />
  </div>
</template>
<script>
import HeaderTitle from '@/components/HeaderTitle'
import CustomerForm from '@/components/CustomerForm'
import CustomerDialog from '@/components/CustomerDialog'
import Pagination from '@/components/Pagination'
import mixin from '@/mixin/mixin'
export default {
  components: {
    HeaderTitle,
    CustomerForm,
    CustomerDialog,
    Pagination
  },
  mixins: [mixin],
  data() {
    return {
      title: '客户列表',
      visible: false,
      page: 1,
      pagesize: 10,
      row: {},
      loading: false,
      selectData: {}
    }
  },
  mounted() {
    this.getCustomerData({
      page: this.page,
      rows: this.pagesize
    })
    this.getCarNameData({ type: 0 })
  },
  methods: {
    getIndex(index) {
      return index + (this.page - 1) * this.pagesize + 1
    },
    handleSubmit(playload) {
      this.loading = true
      this.selectData = playload
      this.getCustomerData({
        page: this.page,
        rows: this.pagesize,
        ...playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    handleClick(row) {
      let newRow = {}
      if (row.id) {
        newRow = { ...row }
      }
      this.row = newRow
      this.visible = true
    },
    handleClickPage(page) {
      this.loading = true
      this.page = page
      this.getCustomerData({
        page: page,
        rows: this.pagesize,
        ...this.selectData
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        } else {
          this.$message.error(res.state.msg)
        }
      })
    },
    handleSizeChange(pagesize) {
      this.loading = true
      this.pagesize = pagesize
      this.getCustomerData({
        page: this.page,
        rows: this.pagesize,
        ...this.selectData
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        } else {
          this.$message.error(res.state.msg)
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
    handleConfirm(playload) {
      this.loading = true
      this.editCustomerData(playload).then((res) => {
        if (res.data.state === '000') {
          this.$message.success('编辑成功！')
          this.loading = false
          this.getCustomerData({
            page: this.page,
            rows: this.pagesize,
            ...this.selectData
          })
        } else {
          this.$message.error(res.data.msg)
        }
      })
      this.visible = false
    }
  }
}
</script>
<style scoped>
</style>

